#include "matcher.h"

Matcher::Matcher()
{
}

Matcher::~Matcher()
{
}

int Matcher::getOperatorPrecedence(string op)
{
	if (matchExact(op,"+")||matchExact(op,"-"))
		return 2;
	else if (matchExact(op,"*")||matchExact(op,"/"))
		return 3;
	else if (matchExact(op,"(")||matchExact(op,")"))
		return 1;
	return -1;
}
bool Matcher::matchExact(string s1, string s2)
{
	return s1.compare(s2)==0;
}
bool Matcher::matchConstValue(string s1)
{
	return matchInteger(s1);
}

bool Matcher::matchInteger(string s1)
{
	//char c;
	if (s1.length()<1)
		return false;
	int counter=0;
	bool isDigit=true;
	while (counter<s1.size()&&isDigit)
	{
		isDigit=isDigit&&(isSingleDigit(s1[counter]));
		counter++;
	}
	return isDigit;
}
bool Matcher::isCharacter(char c)
{
	return (((int)c>=(int)'a')&&((int)c<=(int)'z'))||
		(((int)c>=(int)'A'&&(int)c<=(int)'Z'));
}

bool Matcher::isSingleDigit(char c)
{
	return (((int)c<=(int)'9')&&((int)c>=(int)'0'));
}
bool Matcher::isHash(char c)
{
	return (int)c==(int)'#';
}

bool Matcher::matchName(string s1)
{
	if (s1.length()<=0)
		return false;
	if (s1.length()==1)
		return isCharacter(s1[0]);
	if (s1.length()>1)
	{
		bool isName=true;
		isName=(isName&&isCharacter(s1[0]));
		int counter=1;
		while (counter<s1.length()&&isName)
		{
			isName=isName&&(isSingleDigit(s1[counter])||isCharacter(s1[counter]));
			counter++;
		}
		return isName;
	}
	return false;
}

bool Matcher::matchProcedureName(string s1)
{
	return matchName(s1);
}
bool Matcher::matchVarName(string s1)
{
	return matchName(s1);
}
bool Matcher::matchIden(string s1)
{
	if (s1.length()<=0)
		return false;
	if (s1.length()==1)
		return isCharacter(s1[0]);
	if (s1.length()>1)
	{
		bool isName=true;
		isName=(isName&&isCharacter(s1[0]));
		int counter=1;
		while (counter<s1.length()&&isName)
		{
			isName=isName&&(isSingleDigit(s1[counter])||isCharacter(s1[counter])||isHash(s1[counter]));
			counter++;
		}
		return isName;
	}
	return false;
}

bool Matcher::matchOperator(string s1)
{
	if (s1.compare("+")==0)
	{
		return true;
	}
	else if (s1.compare("-")==0)
	{
		return true;
	}
	else if (s1.compare("*")==0)
	{
		return true;
	}
	else if (s1.compare("/")==0)
	{
		return true;
	}
	else
	{
		return false;
	}
}
